package com.service;

import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.data.redis.core.StringRedisTemplate;
import org.springframework.scheduling.annotation.Async;
import org.springframework.stereotype.Service;

import java.util.concurrent.TimeUnit;

@Service
public class RenewRedisLock {
    @Autowired
    private StringRedisTemplate stringRedisTemplate;

    @Async
    public void renewRedisLock(String key, String value, int seconds) throws InterruptedException {
        String inRedisValue = stringRedisTemplate.opsForValue().get(key);
        if (StringUtils.isNotEmpty(inRedisValue) && inRedisValue.equals(value)) {
            stringRedisTemplate.expire(key, seconds, TimeUnit.SECONDS);
            TimeUnit.SECONDS.sleep(10);
            renewRedisLock(key, value, seconds);
        } else {
            return ;
        }
    }
}
